	//Tranz-Am
#include "game_transam.h"

//local game specifics
static bool newLevelRequired=true;	//new level

static float mapXoff = 0;		//place in map we are at (top left)
static float mapYoff = 0;

//logic called 1/FPS seconds
bool TransAmGameLogic()
{
	//has a new level started
	//if yes, then set the starting point to be the middle of the map
	if(newLevelRequired) 
	{
		newLevelRequired=false;
		if(!NewLevel())		//load map
		{
			Configuration::GlobalErrorString="Could not initialise map. Check log file and the transam.fmp file exists.";
			return true;
		}
		mapXoff=(mapblockwidth*mapwidth)/2;
		mapYoff=(mapblockheight*mapheight)/2;
	}

	//do keyboard handling
	//update player logic which can quit if ESCape pressed
	if(key[KEY_ESC])
	{
		CleanUp();
		return true;
	}
	//these variables are used for determining which part of the map to show
	if(key[KEY_LEFT]) mapXoff-=8;
	if(key[KEY_RIGHT]) mapXoff+=8;
	if(key[KEY_UP]) mapYoff-=8;
	if(key[KEY_DOWN]) mapYoff+=8;

	//ensure we don't go past the edges
	//lowercase variables are mappy variables
	if(mapXoff+ShownMapWidth>(mapblockwidth*mapwidth-1))
		mapXoff-=8;
	if(mapXoff<0) mapXoff=0;
	if(mapYoff+ShownMapHeight>(mapblockheight*mapheight-1))
		mapYoff-=8;
	if(mapYoff<0) mapYoff=0;
	//any mappy stored animations are updated
	MapUpdateAnims();
	return false;
}

//drawing loop, called 1/FPS or when ready if running slow
bool TransAmGameDrawing()
{
	clear(GameFramework->DrawingSurface);

	//score panel - always top left. maybe only show if screen is > certain size
	blit(DisplayPanel->ReadOnly_CurrentItem,GameFramework->DrawingSurface,0,0,0,0,DisplayPanel->ReadOnly_CurrentItem->w,DisplayPanel->ReadOnly_CurrentItem->h);


	//mappy function to draw the relevant layers
	//draw to the location set up at the start (to the right of the panel)
	//draw to fill the remainder of the screen
	//set starting point within the map to show - at the start it is the approximate centre

	//draw map background
	MapDrawBGT (GameFramework->DrawingSurface, (int)mapXoff, (int)mapYoff, ScreenXOffset, ScreenYOffset, ShownMapWidth, ShownMapHeight);
	//draw foreground stuff
	MapDrawFG (GameFramework->DrawingSurface, (int)mapXoff, (int)mapYoff, ScreenXOffset, ScreenYOffset, ShownMapWidth, ShownMapHeight, 0);

	textprintf_ex(GameFramework->DrawingSurface,font,0,0,makecol(255,255,255),0,"mapXoff %f mapYoff %f maxw %d maxh %d",mapXoff,mapYoff,mapblockwidth*mapwidth,mapblockheight*mapheight);
	textprintf_ex(GameFramework->DrawingSurface,font,180,10,makecol(255,255,255),0,"Version 2: just the map. Cursors move. ESCape quits");
	return false;
}

/**********************************************
	local helper functions
*/

//ending, clean up stuff we created
static void CleanUp()
{
	//mappy - destroy any memory allocated and release the map
	MapFreeMem();
}

static bool NewLevel()
{
	//starts the level
	//does not perform resetting of all data as may be just a new life
	CleanUp();
	
	int loaded=1; //default to not loaded
	
	//initial map file, try VRAM if paging or triple buffering
	//sort out levels later, for now just one
	if(GameConfiguration->CapsActualSystem.GraphicsMode==MODE_PAGED || GameConfiguration->CapsActualSystem.GraphicsMode==MODE_TRIPLE)
	{
		//try VRAM as buffer is video
		loaded=MapLoadVRAM("transam.fmp");
		if(loaded!=0) Configuration::LogEntry("Buffer is VRAM but could not load map into vram, using standard");
		else Configuration::LogEntry("loaded map into VRAM");
	}
	if(loaded!=0)
	{
		loaded=MapLoad ("transam.fmp");
		if(loaded!=0) Configuration::LogEntry("Buffer is not VRAM but could not load map into memory! crikey!");
		else Configuration::LogEntry("loaded map into memory");
	}
	if(loaded!=0) return false;
	
	//mappy initialisation
	MapGenerateYLookup();	//speed up direct array access to block data
	MapInitAnims ();		//initialise all mappy animations
	
	return true;	
}

